package think.in.spring.ioc.overview.container;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import think.in.spring.ioc.overview.domain.User;

import java.util.Arrays;

public class IoCContainerDemo {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(IoCContainerDemo.class);
        User u = context.getBean(User.class);
        System.out.println(Arrays.toString(context.getBeanDefinitionNames()));
        System.out.println(context.getBeanDefinitionCount());
        System.out.println(u);
    }

    private static void classPathXml() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/dependency-lookup-context.xml");
        User u = context.getBean(User.class);
        System.out.println(Arrays.toString(context.getBeanDefinitionNames()));
        System.out.println(context.getBeanDefinitionCount());
        System.out.println(u);
    }

    @Bean
    public User user(){
        User u = new User();
        u.setId(1L);
        u.setName("spring");
        return u;
    }

    private static void beanFactoryDemo() {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        int beanDefCount = reader.loadBeanDefinitions("classpath:META-INF/dependency-lookup-context.xml");
        System.out.println(beanDefCount);
        User bean = beanFactory.getBean(User.class);
        System.out.println(bean);
    }
}
